const handleDate = require('../../utils/util.js').handleDate;
const app = getApp()

Page({
  data: {
    hours: '00',
    minutes: '00',
    seconds: '00',
    userInfo:{},
    startTime:'0000-00-00 00:00:00',
    clickCount: 0,
    validCount: 0
  },
  onLoad: function () {
    let self = this;
    wx.getStorage({
      key: 'userInfo',
      success(res) {
        self.setData({userInfo:　res.data})
      }
    })
  },
  makePoint(){
    this.setData({ clickCount: ++this.data.clickCount })
    if (this.data.clickCount === 1){
      this.timerCount();
    }
    if(this.data.clickCount === 1){
      this.setData({ validCount: 1 })
      this.setData({ startTime: handleDate('yyyy-MM-dd HH:mm:ss', new Date()) })
    }
    if ((new Date().getTime() - new Date(this.data.startTime).getTime())/1000/60 > 5){
      this.setData({ validCount: ++this.data.validCount })
    }
  },
  timerCount(){
    this.setData({ minutes: 59, seconds: 59});
    var second = 60;
    var minute = 59
    let timer = setInterval(()=>{
      --second;
      if (second < 10 && second > 0){
        this.setData({ seconds: '0' + second })
      } else if (second === 0){
        second = 60;
        --minute;
        if (minute < 0){
          clearInterval(timer);
          this.setData({ minutes: '00', seconds: '00' })
        }else{
          this.setData({ seconds: '00' })
          if (minute < 10 && minute >= 0) {
            this.setData({ minutes: '0' + minute });
          } else if (minute <= 59 && minute >= 10) {
            this.setData({ minutes: minute })
          } else if (second === 0) {
            this.setData({ minutes: '00' });
          }
        } 
      } else if (second >= 10 && second <= 59){
        this.setData({ seconds: second })
      } 
    },1000);
  },
  toPage(e){
    if(e.target.dataset.page){
      wx.navigateTo({
        url: '../fetalMovementInfo/fetalMovementInfo'
      })
    }
  },
  submitRecord(){
    if ((new Date().getTime() - new Date(this.data.startTime).getTime()) / 1000 / 60 < 15) {
      wx.showModal({
        title: '提示',
        content: '本次胎动记录时间小于15分钟，将不保存此次记录，是否确认结束？',
        confirmColor: '#84D1CB',
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }else{
      
    } 
  }
})
